import requests
import re
from bs4 import BeautifulSoup
i = 1
def catchBook(response):
    soup = BeautifulSoup(response.text,'html.parser')
    for book_name,book_url in zip(soup.select('.pl2'),soup.select('.pl2 a')):
         global i
         name_clean_data = re.sub(r'\s+', ' ', book_name.text.strip())
         url_clean_data = re.sub(r'\s+', ' ', book_url['href'])
         print(f'{i}:',name_clean_data,"url:",url_clean_data)
         i+=1

#目标网站：豆瓣读书
url = 'https://book.douban.com/top250?start='

#伪造为浏览器
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

#获取HTML文本
response = requests.get(url,headers=headers)
if response.status_code == 200:
    print('请求成功！')
    for num in range(0,10):
        newurl = url+str(num*25)
        print(newurl)
        newresponse = requests.get(newurl,headers=headers)
        catchBook(newresponse)
else :
        print('请求失败！')
